package com.lishe.lishe.lishe.module.main.http;

import com.lishe.lishe.asynchttp.HttpUtils;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.main.bean.ApkMessage;
import com.lishe.lishe.lishe.module.main.bean.NoticeObject;
import com.lishe.lishe.lishe.module.main.interfaces.HttpInterface;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by lwa on 2017/9/4.
 * app升级接口
 */

public class HttpAppUpdata {

    //数据所在的表
    private static final String TABLE_PAY = "Pay/";
    private static int number=0;//请求接口次数
    /**
     * apk下载
     * @param token
     * @param url 下载链接
     */
    public static void downLoadApk(String token,String url,ResponseHandlerInterface response){
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("url", url);
        HttpUtils.post(Globals.LISHE_URL +"Home/"+TABLE_PAY + "createPayOrderInfo/", params, response);
    }

    /**
     * apk安装版本检测
     * @param type 请求的手机系统 1（android） 2（ios）
     */
    public static void checkApkVersion(int type, final DataResponseListener<ApkMessage> listener){
        RetrofitFactory.create().create(HttpInterface.class).getNewVersion(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<ApkMessage>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<ApkMessage> requestBody) {
                        if(requestBody.getResult()==100&&requestBody.getErrcode()==0){
                            listener.onSuccess(requestBody.getData());
                        }else{

                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 设置设备别名与标签
     * @param re_id 极光推送RegistrationID
     */
    public static void updateDevice(final String re_id){
        number++;
        String alias= SharedPreferencesUtils.getPhone(null);
        String mobile=null;
        String addTags=null;
        String removeTags=null;
        RetrofitFactory.create().create(HttpInterface.class).updateDevice(re_id,alias,mobile,addTags,removeTags)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody requestBody) {
                        if(requestBody.getResult()==100&&requestBody.getErrcode()==0){
                            number=0;
                            SharedPreferencesUtils.saveRegistrationID("");
                        }else{
                            if(number<=3)
                                updateDevice(re_id);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取通告
     */
    public static void getNotice(final DataResponseListener<List<NoticeObject>> listener){
        RetrofitFactory.create().create(HttpInterface.class)
                .getNotice()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<List<NoticeObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<List<NoticeObject>> requestBody) {
                        if(requestBody.getResult()==100&&requestBody.getErrcode()==0){
                            listener.onSuccess(requestBody.getData());
                        }else{
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
